package tech.daoxuan.book.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.daoxuan.book.dao.UserRepository;
import tech.daoxuan.book.entity.User;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Optional;

@RestController("/user")
public class UserControler {

    @Autowired
    UserRepository userRepository;

    @PostMapping("/add")
    public User add(User user){
        User save = userRepository.save(user);
        return save;
    }
    @GetMapping("/all")
    public List<User> getAll(){
        List<User> all = userRepository.findAll();
        return all;
    }

    @GetMapping("/{id}")
    public User getById(@PathParam("{id}") int id){
        Optional<User> byId = userRepository.findById(id);
        if (byId.get()!=null){
            return byId.get();
        }
        return null;
    }
}
